package com.example.forum.service.impl;

import com.example.forum.common.ResultCode;
import com.example.forum.dao.ArticleMapper;
import com.example.forum.dao.ArticleReplyMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.service.IArticleReplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleReplyServiceImpl implements IArticleReplyService {

    @Autowired
    private ArticleReplyMapper articleReplyMapper;

    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public Integer createArticleReply(Long id,String content,Long userId) {
        if(id == null || !StringUtils.hasLength(content)) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
        }

        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticle_id(id);
        articleReply.setContent(content);
        //默认设置值
        articleReply.setReply_id(0L);
        articleReply.setPost_user_id(userId);
        articleReply.setReply_user_id(0L);
        articleReply.setState(0);
        articleReply.setDelete_state(0);
        articleReply.setLike_count(0);
        articleReply.setCreate_time(new Date(System.currentTimeMillis()));
        articleReply.setUpdate_time(new Date(System.currentTimeMillis()));
        Integer ret = articleReplyMapper.insert(articleReply);

        Article article = articleMapper.selectByPrimaryKey(id);
        if(article == null) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        article.setReply_count(article.getReply_count() + 1);
        Integer ret1 = articleMapper.updateByPrimaryKeySelective(article);
        if(ret == null || ret1 == null) {
            log.warn("插入帖子回复失败");
            throw new ApplicationException("插入帖子回复失败");
        }
        log.info("插入帖子回复成功");
        return ret;
    }

    @Override
    public List<ArticleReply> getAllArticleReply(Long articleId) {
        if(articleId == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
        }
        log.info("回复帖子列表获取成功");
        return articleReplyMapper.selectByArticleId(articleId);

    }

    @Override
    public Article selectByArticleId(Long articleId) {
        Article article = articleMapper.selectByPrimaryKey(articleId);
        if(article == null) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }

        log.info("查找文章成功");
        return article;
    }
}
